package com.meng.redis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.Set;


/**
 * 测试jedis 操作 redis  是否成功  连接池等等。。。后面去了解
 *
 * 阿里云 集群是否搭建成功
 *
 *
 * Jedis中操作String，List，Map，以及集合排序
 *
 * @author MENG
 * @version 2017/12/5
 * @see
 */
public class TestJedis
{
    //Redis 集群需要使用 JedisCluster对象
//    private static Jedis jedis = new Jedis("47.104.22.231", 6379);


    private static GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();


    private static JedisCluster jedisCluster;

    static
    {
        String[] serverArray = ("47.104.22.231:6379," +
                                "47.104.22.231:6380," +
                                "47.104.22.231:6381," +
                                "47.104.22.231:6382," +
                                "47.104.22.231:6383," +
                                "47.104.22.231:6384").split(",");

        Set<HostAndPort> nodes = new HashSet<>();

        poolConfig.setMaxTotal(200);

        poolConfig.setMaxIdle(50);
        poolConfig.setMinIdle(8);
        poolConfig.setMaxWaitMillis(-1);

        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
//Idle时进行连接扫描
        poolConfig.setTestWhileIdle(true);
//表示idle object evitor两次扫描之间要sleep的毫秒数
        poolConfig.setTimeBetweenEvictionRunsMillis(30000);
//表示idle object evitor每次扫描的最多的对象数
        poolConfig.setNumTestsPerEvictionRun(10);
//表示一个对象至少停留在idle状态的最短时间，然后才能被idle object evitor扫描并驱逐；这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
        poolConfig.setMinEvictableIdleTimeMillis(60000);


        for (String ipPort : serverArray)
        {
            String[] ipPortPair = ipPort.split(":");

            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));

            jedisCluster = new JedisCluster(nodes,poolConfig);
        }

        jedisCluster = new JedisCluster(nodes,20000,poolConfig);


    }



    public void operateString()
    {
        if(jedisCluster != null)
        {
            System.out.println("Redis 连接成功...");
        }

        System.out.println(jedisCluster.get("MENG"));

        System.out.println(jedisCluster.get("meng"));

    }


    public static void main(String[] args)
    {
        TestJedis testJedis = new TestJedis();

        testJedis.operateString();

        System.out.println(JedisUtils.JedisClusterKeys(TestJedis.jedisCluster,"*"));
    }

}
